import { createRouter, createWebHashHistory } from 'vue-router'
import store from '../store/store'
import routers from './routers'


const router = createRouter({
    history: createWebHashHistory(),
    routes: routers,
    // 保持滚动条位置
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            return { top: 0 }
        }
    },
})

// 导航钩子，全局钩子
router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        document.title = to.meta.title
    }
    // 登录页面
    if (to.name === 'login') {
        next();
    }else {
        if (store.getters.token == '' && to.meta.requiresAuth) {
            next('/login');
        } else {
            next();
        }
    }
})
export default router
